"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const turf_1 = __importDefault(require("../../../ext/turf"));
const ProgressBar_1 = __importDefault(require("../ProgressBar/ProgressBar"));
const arrChunk_1 = __importDefault(require("../arrChunk"));
const pg = new ProgressBar_1.default('聚合多边形', 40);
let featuresLength = 0;
let completedIndex = 0;
function union(sourceGeoJson) {
    let resGeoJson;
    turf_1.default.featureEach(sourceGeoJson, function (currentFeature, featureIndex) {
        if (featureIndex === 0) {
            resGeoJson = currentFeature;
        }
        else {
            resGeoJson = turf_1.default.union(resGeoJson, currentFeature);
        }
        completedIndex++;
        pg.render({
            completed: completedIndex,
            total: featuresLength,
            description: '聚合多边形'
        });
    });
    return resGeoJson;
}
function unionGeoJson(geoJson) {
    const sourceGeoJson = turf_1.default.flatten(geoJson);
    featuresLength = sourceGeoJson.features.length;
    completedIndex = 0;
    const group = [];
    if (featuresLength > 200) {
        const chunkSize = Math.ceil(featuresLength / 10);
        const chunk = (0, arrChunk_1.default)(sourceGeoJson.features, chunkSize);
        for (let i = 0; i < chunk.length; i++) {
            group.push(chunk[i]);
        }
    }
    else {
        group.push(sourceGeoJson.features);
    }
    const groupGeoJson = [];
    for (let i = 0; i < group.length; i++) {
        const collection = turf_1.default.featureCollection(group[i]);
        groupGeoJson.push(union(collection));
    }
    let sumGeoJson = union(turf_1.default.featureCollection(groupGeoJson));
    return turf_1.default.flatten(sumGeoJson);
}
exports.default = unionGeoJson;
